﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tron.DataStructures;
using Microsoft.Xna.Framework;
using Tron.Players.Simple;

namespace Tron.Players.Strategic
{
    class StrategicPlayer : Player
    {
        StrategicPlayerHelper strategicPlayerHelper = new StrategicPlayerHelper();

        public StrategicPlayer(int playerIndex)
            : base(playerIndex)
        { }

        public override void Update(Percepts percepts, Actions actions)
        {
            //reflexAgentPlayer.Update(percepts, actions);

            int[,] board = percepts.Board.GetCopyOfBoardArray();

            int opponentIndex = -1;

            if (PlayerIndex == 0)
            {
                opponentIndex = 1;
            }
            else
            {
                opponentIndex = 0;
            }

            Vector2 playerPosition = percepts.PlayerStates[PlayerIndex].Position;
            Vector2 opponentPosition = percepts.PlayerStates[opponentIndex].Position;

            bool opponentAlive = percepts.PlayerStates[opponentIndex].Alive;

            //List<Vector2> squaresThatCanBeReachedBeforeOpponent = new List<Vector2>();

            List<Vector2> playerPossibleMoveVectors = strategicPlayerHelper.GetPossibleMoveVectorsOnBoardClockWiseFromDirection(board, playerPosition, actions.MoveDirection);
            List<Vector2> opponentPossibleMoveVectors = opponentAlive ? strategicPlayerHelper.GetPossibleMoveVectorsOnBoard(board, opponentPosition) : new List<Vector2>();

            double maxNum = -2;
            Vector2 maxValue = Vector2.Zero;
            if (playerPossibleMoveVectors.Count == 0)
            {
                // Crashing is inevitable
            }
            else if (playerPossibleMoveVectors.Count == 1)
            {
                actions.MoveDirection = playerPossibleMoveVectors[0];
            }
            else
            {
                for (int i = 0; i < playerPossibleMoveVectors.Count; i++)
                {
                    for (int j = 0; j < opponentPossibleMoveVectors.Count; j++)
                    {
                        if (playerPossibleMoveVectors.Count > 0)
                        {
                            if (playerPosition + playerPossibleMoveVectors[i] == opponentPosition + opponentPossibleMoveVectors[j])
                            {
                                playerPossibleMoveVectors.RemoveAt(i);
                                i = 0;
                                j = -1;
                            }
                        }
                    }
                }

                if (playerPossibleMoveVectors.Count == 0)
                {
                    playerPossibleMoveVectors = strategicPlayerHelper.GetPossibleMoveVectorsOnBoard(board, playerPosition);
                }

                foreach (Vector2 playerPossibleMoveVector in playerPossibleMoveVectors)
                {
                    Vector2 newPlayerLocation = playerPosition + playerPossibleMoveVector;
                    double num = strategicPlayerHelper.GetNumberOfSquaresThatCanBeReachedBeforeOtherPlayer(board,
                        newPlayerLocation, opponentPosition, opponentAlive);
                    if (num > maxNum)
                    {
                        maxNum = num;
                        maxValue = playerPossibleMoveVector;
                    }
                }
                actions.MoveDirection = maxValue;
            }
        }
    }
}
